package com.jumi.microservice.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

/**
 * @author scw
 * @Date 2021-01-09 13:40
 */
public class PddCreateSign {
    /**
     * 生成签名
     */
    public static String createSign(SortedMap<String, Object> sortedMap, String key) {
        StringBuilder sb = new StringBuilder();
        sb.append(key);
        Set<Map.Entry<String, Object>> entrySet = sortedMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (v != null && !"".equals(v) && !"sign".equals(k)) {
                sb.append(k).append(v);
            }
        }
        sb.append(key);
        return DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }

    /**
     * Map转String字符串&拼接
     */
    public static String mapToString(SortedMap<String, Object> sortedMap) {

        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, Object>> entrySet = sortedMap.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (v != null && !"".equals(v) && !"sign".equals(k)) {
                sb.append(k).append("=").append(v).append("&");
            }
        }
        return sb.toString();
    }
}
